import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TextJDBC {
    public static void main(String[] args) throws SQLException {
            //让用户输入
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入id: ");
            int id = scanner.nextInt();
            System.out.println("请输入姓名: ");
        String name = scanner.next();
        //1.创建数据源对象
//        DataSource dataSource = new MysqlDataSource();
//        ((MysqlDataSource) dataSource).setUrl();
//        ((MysqlDataSource) dataSource).setUser();
//        ((MysqlDataSource) dataSource).setPassword();
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mdx?characterEncoding=utf8&useSSL=false");
        dataSource.setUser("root");
        dataSource.setPassword("021642");
        //和数据库服务器建立连接
        Connection connection =dataSource.getConnection();
        System.out.println(connection);
        //3.构造SQL语句
        //String sql = "insert into student values("+  id +",'"+ name +"')";
        String sql = "insert into student values(?,?)";

        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);

        //4.把SQL发送到服务器上执行
        int n = statement.executeUpdate();
        System.out.println(n);
        //5.释放上述资源
        statement.close();
        connection.close();
    }
}
